<?php

namespace App\Admin\Controllers;

use App\Models\FhCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FhCategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->c_name->tree();
            $grid->column('c_url');
            $grid->column('c_keyword');
            // $grid->column('c_desc');
            // $grid->column('pid')->display(function ($pid) {
            //     $title = FhCategory::where('id', $pid)->value('c_name');
            //     return $title==""?"顶级":$title;
            // });
            $grid->column('img_url')->image('',100,40);
            $grid->column('show')->switch();
            $grid->column('sort')->editable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('c_name');
        
            });
            $grid->actions(function ($actions) {
                // 隐藏显示按钮
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhCategory(), function (Show $show) {
            $show->field('id');
            $show->field('c_name');
            $show->field('c_url');
            $show->field('c_keyword');
            $show->field('c_desc');
            $show->field('pid');
            $show->field('img_url');
            $show->field('show');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FhCategory(), function (Form $form) {
            $form->display('id');
            $form->text('c_name')->required();
            $form->text('c_url')->required()->help("输入地址如pc,如果类目是顶级类目这需要，添加/project-category/ ,提交前请查看是否是/project-category/XXX/XXX 类型，/project-category/只能出现一次");
            $form->text('c_keyword');
            $form->text('c_desc')->required();
            $form->select('pid')->options(FhCategory::selectOptions())->required();
            $form->image('img_url')->required()->help("图片分辨率最佳758*506");
            $form->switch('show')->default(1);
            $form->number('sort')->default(1);
            
             $form->saving(function (Form $form) {
                $ids = $form->model()->id; 
                if (!$form->model()->id) {
                    $pattern = '/[?\&.\{\}#\s]/';  // 正则表达式
                    $isValid = preg_match($pattern,  $form->c_url) === 1; // 验证逻辑
                    if($isValid){
                        throw new \Exception('路由地址包含?&.{}# 空格特殊字符,请把上述字符替换成减号“-” ');
                    }
                }
                
            });
            
            
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * getTop  获取顶级类目
     * 
     * @return json
     */
    public function getTop(){
        $top = new FhCategory();
        $data = $top->where("pid",0)->select('id','c_name as text')->get();
        return response()->json($data);

    }
}
